package com.study.vueblog.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Profile({"dev"})
@Slf4j
public class StartSysListener implements ApplicationListener<ContextRefreshedEvent> {
    private MyBatisInterceptor executorInterceptor = new MyBatisInterceptor();

    @Resource
    private List<SqlSessionFactory> sqlSessionFactoryList;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        this.addMyInterceptor();
    }
    private void addMyInterceptor(){
        log.debug("Adding a Custom Mybatis SQL Interceptor.");
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList){
            sqlSessionFactory.getConfiguration().addInterceptor(executorInterceptor);
        }
    }
}
